<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="题目" prop="questionTitle">
        <el-input
          v-model="queryParams.questionTitle"
          placeholder="请输入题目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yljf:baseQuestionBank:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yljf:baseQuestionBank:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yljf:baseQuestionBank:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yljf:baseQuestionBank:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table width="100%" v-loading="loading" :data="baseQuestionBankList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键 id" align="center" prop="id" /> -->
      <el-table-column width="400" label="题目" align="center" prop="questionTitle" >
        <template slot-scope="scope">
          <div class="cell-wrap">
            <div class="cell-content">{{scope.row.questionTitle}}</div>
            <div class="pick-item" v-for="item in scope.row.questionOptionsList">{{item}}</div>
          </div>
        </template>
      </el-table-column>
     <!-- <el-table-column width="200" label="选项" align="center" prop="questionOptionsTxt">
        <template slot-scope="scope">
          <div class="cell-content">{{scope.row.questionOptionsTxt}}</div>
        </template>
      </el-table-column> -->
      <el-table-column label="正确答案" align="center" prop="correctAnswer" />
     <el-table-column label="易错选项" align="center" prop="errorAnswer" />
      <el-table-column label="正确率" align="center" prop="ac" />
      <el-table-column width="300" label="解析" align="center" prop="questionText" >
        <template slot-scope="scope">
          <div class="cell-content">{{scope.row.questionText}}</div>
        </template>
      </el-table-column>
      <el-table-column label="是否单选" align="center" prop="questionType" />
      <el-table-column width="150" label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yljf:baseQuestionBank:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yljf:baseQuestionBank:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基础题库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="题目" prop="questionTitle">
          <el-input v-model="form.questionTitle" placeholder="请输入题目" />
        </el-form-item>
        <el-form-item label="A选项" prop="A" >
          <el-input v-model="form.A" placeholder="请输入A选项" />
        </el-form-item>
        <el-form-item label="B选项" prop="B">
          <el-input v-model="form.B" placeholder="请输入B选项"  />
        </el-form-item>
        <el-form-item label="C选项" prop="C">
          <el-input v-model="form.C" placeholder="请输入C选项"  />
        </el-form-item>
        <el-form-item label="D选项" prop="D">
          <el-input v-model="form.D" placeholder="请输入D选项" />
        </el-form-item>

        <el-form-item label="正确选项" prop="correctAnswer" >
          <!-- <el-input v-model="form.correctAnswer" placeholder="请输入正确答案" /> -->
          <el-select multiple v-model="form.correctAnswer" style="width: 100%;" placeholder="请选择正确答案" clearable>
            <el-option
              v-for="dict in correctAnswerList"
              :key="dict"
              :label="dict"
              :value="dict"
            />
          </el-select>


        </el-form-item>
        <el-form-item label="易错选项" prop="errorAnswer">
          <el-select multiple v-model="form.errorAnswer" style="width: 100%;" placeholder="请选择易错选项" clearable>
            <el-option
              v-for="dict in correctAnswerList"
              :key="dict"
              :label="dict"
              :value="dict"
            />
          </el-select>
          <!-- <el-input v-model="form.errorAnswer" placeholder="请输入易错选项" /> -->
        </el-form-item>
        <el-form-item label="正确率" prop="ac">
          <el-input-number style="text-align: left;"  v-model="form.ac" controls-position="right"  placeholder="请输入正确率" :min="1" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="解析" prop="questionText">
          <el-input v-model="form.questionText" placeholder="请输入解析" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBaseQuestionBank, getBaseQuestionBank, delBaseQuestionBank, addBaseQuestionBank, updateBaseQuestionBank } from "@/api/yljf/baseQuestionBank";

export default {
  name: "BaseQuestionBank",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基础题库表格数据
      baseQuestionBankList: [],
      // 弹出层标题
      title: "",
      correctAnswerList:['A','B','C','D'],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        questionTitle: null,
        questionOptions: null,
        correctAnswer: null,
        errorAnswer: null,
        ac: null,
        questionText: null,
        questionType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询基础题库列表 */
    getList() {
      this.loading = true;
      listBaseQuestionBank(this.queryParams).then(response => {
        this.baseQuestionBankList = response.rows.map(item=>{
          item.questionOptionsList = this.getQuestionOptionsList(JSON.parse(item.questionOptions))
          return item
        });
        this.total = response.total;
        this.loading = false;
      });
    },

    getQuestionOptionsList(questionOptions){
      var arr = []
      for (var key in questionOptions) {
        arr.push(key+'.'+questionOptions[key])
      }
      return arr
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        questionTitle: null,
        questionOptions: null,
        correctAnswer: null,
        errorAnswer: null,
        ac: null,
        questionText: null,
        questionType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加基础题库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBaseQuestionBank(id).then(response => {
        this.form = {
          ...response.data,
          ...JSON.parse(response.data.questionOptions),
          correctAnswer:response.data.correctAnswer.split(" "),
          errorAnswer:response.data.errorAnswer.split(" "),
          ac:parseInt(response.data.ac)
        };

        console.log(this.form)


        this.open = true;
        this.title = "修改基础题库";
      });
    },

    questionOptionsChange(e){
      console.log(e,this.form)


      if(this.form.questionOptions){
        this.form.questionOptions
      }
    },


    /** 提交按钮 */
    submitForm() {
      if(!this.form.A || !this.form.B || !this.form.C || !this.form.D){
        this.$modal.msgError("请输入题目的选项内容");
        return
      } else {
        let set = new Set([this.form.A, this.form.B, this.form.C, this.form.D]);

        if (set.size < 4) {
             this.$modal.msgError("不能出现一样的选项内容");
             return
        }
      }


      this.form.questionOptions = JSON.stringify({
        A:this.form.A,
        B:this.form.B,
        C:this.form.C,
        D:this.form.D,
      })

      // delete this.form.A
      // delete this.form.B
      // delete this.form.C
      // delete this.form.D

      console.log(this.form)

      // return
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.questionType = this.form.correctAnswer.length > 1 ? '多选' : '单选'
          this.form.correctAnswer = this.form.correctAnswer.join(" ")
          this.form.errorAnswer = this.form.errorAnswer.join(" ")
          this.form.ac = this.form.ac+'%'

          if (this.form.id != null) {
            updateBaseQuestionBank(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBaseQuestionBank(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除基础题库编号为"' + ids + '"的数据项？').then(function() {
        return delBaseQuestionBank(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yljf/baseQuestionBank/export', {
        ...this.queryParams
      }, `baseQuestionBank_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>

  .cell-wrap{
    display: flex;
    flex-direction: column;
  }

  .cell-content{
      // display: -webkit-box;
      // -webkit-box-orient: vertical;
      // -webkit-line-clamp: 3; /* 限制行数为3 */
      // overflow: hidden;
      display: flex;
      text-align: left;
  }

  .pick-item{
    display: flex;
  }

</style>
